package com.charonmc.commands;

import com.charonmc.CharonCore;
import com.charonmc.utils.mysql.SqlManager;
import lombok.SneakyThrows;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

public class UnNick implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        if (sender.hasPermission("Core.Nick")){
            if (sender instanceof Player){
                Player player = (Player) sender;
                player.setDisplayName(player.getName());
                player.setPlayerListName(player.getName());
                Nick.refreshNick(player);
                player.sendMessage("§7 已取消匿名");
                BukkitRunnable deleteNick = new BukkitRunnable() {
                    @SneakyThrows
                    @Override
                    public void run() {
                        SqlManager SqlManager = new SqlManager("charonmc");
                        SqlManager.createDelete()
                                .inTable("nick")
                                .addCondition("nickname",player.getDisplayName())
                                .build()
                                .execute();
                        SqlManager.close();

                    }
                };
                deleteNick.runTaskAsynchronously(CharonCore.getInstance());

                //其他地方的指令
            } else {
                sender.sendMessage("§c§l该命令只能由玩家执行！");
            }
            //无权限
        } else {
            sender.sendMessage("§c§l你没有使用该命令的权限！");
        }
        return true;
    }
}
